/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.Attribute;
import com.kaaction.modules.mer.entity.AttributeValue;
import com.kaaction.modules.mer.entity.Brand;
import com.kaaction.modules.mer.entity.Color;
import com.kaaction.modules.mer.entity.Size;
import com.kaaction.modules.mer.entity.Sku;
import com.kaaction.modules.mer.entity.Spu;
import com.kaaction.modules.mer.entity.Supplier;
import com.kaaction.modules.mer.service.AttributeService;
import com.kaaction.modules.mer.service.AttributeValueService;
import com.kaaction.modules.mer.service.BrandService;
import com.kaaction.modules.mer.service.ColorService;
import com.kaaction.modules.mer.service.SizeService;
import com.kaaction.modules.mer.service.SkuService;
import com.kaaction.modules.mer.service.SpuService;
import com.kaaction.modules.mer.service.SupplierService;

/**
 * 商品分类Controller
 * @author huanghaiming
 * @version 2017-05-19
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/spu")
public class SpuController extends BaseController {

	@Autowired
	private SpuService spuService;
	
	@Autowired
	private BrandService brandService;
	
	@Autowired
	private SupplierService supplierService;
	
	@Autowired
	private AttributeService attributeService;
	
	@Autowired
	private AttributeValueService attributeValueService;
	
	@Autowired
	private ColorService colorService;
	
	@Autowired
	private SizeService sizeService;
	
	@Autowired
	private SkuService skuService;
	
	@ModelAttribute
	public Spu get(@RequestParam(required=false) String id,String classifyId,String idFlag) {
		Spu entity = null;
		if(!StringUtils.isNotBlank(idFlag)){
			Attribute attr= new Attribute();
			if (StringUtils.isNotBlank(id)){
				entity = spuService.get(id);
				
				String  tag = entity.getTag();
				if(StringUtils.isNotBlank(tag)){
					entity.setTag5(tag.split(","));
				}
				
				String  tag1 = entity.getTag1();
				if(StringUtils.isNotBlank(tag1)){
					entity.setTag6(tag1.split(","));
				}
				
				String  tag2 = entity.getTag2();
				if(StringUtils.isNotBlank(tag2)){
					entity.setTag7(tag2.split(","));
				}
				
				String  tag3 = entity.getTag3();
				if(StringUtils.isNotBlank(tag3)){
					entity.setTag8(tag3.split(","));
				}
				
				String  tag4 = entity.getTag4();
				if(StringUtils.isNotBlank(tag4)){
					entity.setTag9(tag4.split(","));
				}
				
				addAttr(id,classifyId);
				attr.setClassifyId(classifyId);
				attr.setSpuId(id);
				entity.setAttrList(attributeService.findList(attr));
				Sku sku = new Sku();
				sku.setSpuId(id);
				entity.setEventSkuList(skuService.findList(sku));
			}
			if (entity == null){
				entity = new Spu();
				attr.setClassifyId(classifyId);
				attr.setSpuId(id);
				entity.setAttrList(attributeService.selectList(attr));
			}
			//if (StringUtils.isNotBlank(classifyId)){
				Brand brand= new Brand();
				//brand.setClassifyId(classifyId);
				entity.setBrandList(brandService.findList(brand));
				Supplier Supplier= new Supplier();
				//Supplier.setClassifyId(classifyId);
				entity.setSupplierList(supplierService.findList(Supplier));
			//}
			entity.setColorList(colorService.findList(new Color()));
			entity.setSizeList(sizeService.findList(new Size()));
		}else{
			entity = new Spu();
			entity.setId(idFlag);
		}
		
		return entity;
	}
	
	private void addAttr(String spuId, String classifyId) {
	
		Map<String,Object> map = new HashMap<String,Object>();
		AttributeValue attr = null;
		
		map.put("spuId", spuId);
		map.put("classifyId", classifyId);
		List<Map<String,String>> ids = attributeService.selectIds(map);
		
		for(Map<String,String> id :ids){
			attr = new AttributeValue();
			attr.setAttributeId(id.get("id"));
			attr.setSpuId(spuId);
			attributeValueService.save(attr);
		}
		
	}

	@RequiresPermissions("mer:spu:view")
	@RequestMapping(value = {"list", ""})
	public String list(Spu spu, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Spu> page = spuService.findPage(new Page<Spu>(request, response), spu); 
		model.addAttribute("page", page);
		return "modules/mer/spuList";
	}

	@RequiresPermissions("mer:spu:view")
	@RequestMapping(value = "form")
	public String form(Spu spu, Model model) {
		model.addAttribute("spu", spu);
		return "modules/mer/spuForm";
	}

	@RequiresPermissions("mer:spu:edit")
	@RequestMapping(value = "save")
	public String save(Spu spu, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, spu)){
			return form(spu, model);
		}
		List<Sku> listSku = spu.getEventSkuList(); 
		if (listSku.size()<=0){
			model.addAttribute("msg", "属性添加不能为空");
			return form(spu, model);
		}
		
		if(spu.getTag5().length>3){
			model.addAttribute("tag5", "标签不能超过3个");
			return form(spu, model);
		}
		
		/*if(spu.getTag6().length>3){
			model.addAttribute("tag6", "标签不能超过3个");
			return form(spu, model);
		}
		
		if(spu.getTag7().length>3){
			model.addAttribute("tag7", "标签不能超过3个");
			return form(spu, model);
		}
		
		if(spu.getTag8().length>3){
			model.addAttribute("tag8", "标签不能超过3个");
			return form(spu, model);
		}
		
		if(spu.getTag9().length>3){
			model.addAttribute("tag9", "标签不能超过3个");
			return form(spu, model);
		}*/
		spu.setPhotoThumb(spu.getPhoto().replace("/images/", "/_thumbs/images/"));
		spuService.save(spu);
		addMessage(redirectAttributes, "保存商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/spu/?repage";
	}
	
	@RequiresPermissions("mer:spu:edit")
	@RequestMapping(value = "delete")
	public String delete(Spu spu, RedirectAttributes redirectAttributes) {
		spuService.delete(spu);
		addMessage(redirectAttributes, "删除商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/spu/?repage";
	}
	
	@RequiresPermissions("mer:spu:edit")
	@RequestMapping(value = "upOrDown")
	public String upOrDown(Spu spu, RedirectAttributes redirectAttributes) {
		spuService.updateUpOrDown(spu);
		addMessage(redirectAttributes, "上下架成功");
		return "redirect:"+Global.getAdminPath()+"/mer/spu/?repage";
	}
	
	@RequiresPermissions("mer:spu:edit")
	@RequestMapping(value = "upOrDownHome")
	public String upOrDownHome(Spu spu, RedirectAttributes redirectAttributes) {
		spuService.updateUpOrDownHome(spu);
		addMessage(redirectAttributes, "上下首页推荐成功");
		return "redirect:"+Global.getAdminPath()+"/mer/spu/?repage";
	}
	
	
	@RequiresPermissions("mer:spu:edit")
	@RequestMapping(value = "deleteSku",method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Map<String,Object> deleteSku(Spu spu, RedirectAttributes redirectAttributes) {
		return skuService.deleteSku(spu.getId());
	}
	
	
	@RequiresPermissions("mer:operationProductDetail:view")
	@RequestMapping(value = "operationProDtlList")
	public String operationProDtlList(Spu spu, HttpServletRequest request, HttpServletResponse response, Model model) {
		String opeproId = spu.getOpeproId();
		if(StringUtils.isNotBlank(opeproId)){
			spu.setOpeproId(opeproId.split(",")[0]);
		}
		spu.setFlag("1");
		Page<Spu> page = spuService.findPage(new Page<Spu>(request, response), spu); 
		model.addAttribute("page", page);
		return "modules/mer/operationProductDetailForm";
	}
	
	
	@RequiresPermissions("mer:operationContentDetail:view")
	@RequestMapping(value = "operationContentDtlList")
	public String operationContentDtlList(Spu spu, HttpServletRequest request, HttpServletResponse response, Model model) {
		String opecontId = spu.getOpecontId();
		if(StringUtils.isNotBlank(opecontId)){
			spu.setOpecontId(opecontId.split(",")[0]);
		}
		spu.setFlag("2");
		Page<Spu> page = spuService.findPage(new Page<Spu>(request, response), spu); 
		model.addAttribute("page", page);
		return "modules/mer/operationContentDetailForm";
	}
	
}